package com.example.kothin_study.dao;

/**
 * @author cy
 * @date 2024/8/12
 * @apiNote
 */
public class Singleton {
    public static Singleton singleton;

    private Singleton() {
    }

    public synchronized static Singleton getInstance() {
        if (singleton == null) {
            singleton = new Singleton(); // synchronized保证只有一个线程能创建实例，保证了线程安全。
        }
        return singleton;
    }

    public void print() {
        System.out.println("Singleton instance created.");
    }


    public static void main(String[] args) {
//        new Util().doAction1();
//        Util.doAction2();




//        Singleton singleton1 = Singleton.getInstance();
//        singleton1.print();
//
//        Singleton singleton2 = Singleton.getInstance();
//        singleton2.print();
//
//        System.out.println(singleton1 == singleton2); // true
    }
}
